package com.micai.springboot.dao.impl;

import com.micai.springboot.dao.UserDao;
import com.micai.springboot.entity.UserEntity;
import com.micai.springboot.vo.UserCountVo;
import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

/**
 * Created by 赵新国 on 2017/12/4.
 */
@Component
public class UserDaoImpl implements UserDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 创建对象
     * @param user
     */
    @Override
    public void saveUser(UserEntity user) {
        mongoTemplate.save(user);
    }

    /**
     * 根据用户名查询对象
     * @param userName
     * @return
     */
    @Override
    public UserEntity findUserByUserName(String userName) {
        Query query = new Query(Criteria.where("userName").is(userName));
        UserEntity userEntity = mongoTemplate.findOne(query, UserEntity.class);
        return userEntity;
    }

    /**
     * 更新对象
     * @param user
     * @return
     */
    @Override
    public int updateUser(UserEntity user) {
        Query query = new Query(Criteria.where("id").is(user.getId()));
        Update update = new Update()
                .set("userName", user.getUserName())
                .set("passWord", user.getPassWord());
        // 更新查询返回结果集的第一条
        WriteResult writeResult = mongoTemplate.updateFirst(query, update, UserEntity.class);
        // 更新查询返回结果集的所有
        /*mongoTemplate.updateMulti(query, update, UserEntity.class);*/
        if (writeResult != null) {
            return writeResult.getN();
        } else {
            return 0;
        }
    }

    /**
     * 删除,按主键id, 如果主键的值为null,删除会失败
     * @param id
     */
    @Override
    public void deleteUserById(String id) {
        UserEntity userEntity = new UserEntity(id, null, null, null);
        //Query query = new Query(Criteria.where("id").gt(id));
        mongoTemplate.remove(userEntity);
    }

    /**
     * 查询全部对象
     * @return
     */
    @Override
    public List<UserEntity> findUserAll() {
        List<UserEntity> userEntities = mongoTemplate.findAll(UserEntity.class);
        return userEntities;
    }

    @Override
    public UserEntity findUserById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        UserEntity userEntity = mongoTemplate.findOne(query, UserEntity.class);
        return userEntity;
    }

    @Override
    public List<UserCountVo> countUserAll() {
        // 方式一
        Aggregation aggregation = newAggregation(
                match(Criteria.where("passWord").is("123456")),
                group("userName").count().as("total"),
                project("total").and("userName").previousOperation(),
                sort(Sort.Direction.DESC, "total")
        );
        AggregationResults<UserCountVo> groupResults = mongoTemplate.aggregate(aggregation, UserEntity.class, UserCountVo.class);
        return groupResults.getMappedResults();
    }
}
